import 'package:flutter/material.dart';
import 'package:flutter_base_plugin/flutter_base_plugin.dart';
import 'package:flutter_base_plugin_example/app_manager/app_router_manager.dart';

import '../app_manager/app_routers.dart';
import '../config/k_color.dart';
import '../config/k_string.dart';
import '../config/sp_key.dart';
import '../utils/common_utils.dart';

class IndexPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _IndexPageState();
  }
}

class _IndexPageState extends BaseState<IndexPage> {
  bool? mIsFirst;

  ///倒计时工具类
  late TimerUtil timerUtil;
  int mTick = 0; //记录倒计时时间
  int mTotalTick = 3000; //记录倒计时时间
  @override
  void buildCreate() async {
    // TODO: implement anCreate
    initCountDownTimer();
    readCacheData();
  }

  ///初始化倒计时
  initCountDownTimer() {
    timerUtil = TimerUtil(mInterval: 100, mTotalTime: mTotalTick);
    //计时回调
    timerUtil.setOnTimerTickCallback((int tick) {
      setState(() {
        if (tick == 0) {
          ///倒计时完成
          AppRoutesManager.router
              .navigateTo(context, AppRouters.main, clearStack: true);
        }
        mTick = tick;
      });
    });
  }

  ///异步读取缓存数据
  void readCacheData() async {
    ///是否是第一次使用
    // mIsFirst = SpUtil.getBool(SpKey.spFirst);
    mIsFirst = SpUtils.getBool(SpKey.spFirst);
    if (mIsFirst == null || !mIsFirst!) {
      ///保存标示
      SpUtils.putBool(SpKey.spFirst, true);
    } else {
      //非第一次 显示倒计时
      timerUtil.startCountDown();
    }
  }

  @override
  Widget buildWidget(BuildContext context) {
    // TODO: implement buildWidget
    return Scaffold(
      body: Stack(
        children: <Widget>[
          //启动页背景图
          Positioned(
              top: 0,
              bottom: 0,
              child: Image.asset(
                CommonUtils.getImgPath("welcome_bg", format: "jpeg"),
                //图片裁剪方式
                fit: BoxFit.fill,
              )),
          const Positioned(
            right: 0,
            left: 0,
            bottom: 60.0,
            child: Center(
              child: Text(
                KString.cartTabStr,
                style: TextStyle(color: KColors.white, fontSize: 22),
              ),
            ),
          ),
          _buildTimerProgress(),
        ],
      ),
    );
  }

  ///构建倒计时进度条
  Widget _buildTimerProgress() {
    return Positioned(
      right: 20,
      bottom: 60,
      child: SizedBox(
        width: 55,
        height: 55,
        child: InkWell(
          onTap: () {
            //取消倒计时
            if (timerUtil.isActive()) {
              timerUtil.cancel();
            }
            AppRoutesManager.navigateTo(context, AppRouters.main,
                clearStack: true);
          },
          child: Stack(
            children: <Widget>[
              Center(
                child: CircularProgressIndicator(
                  value: (mTotalTick - mTick) / mTotalTick,
                ),
              ),
              Center(
                child: Text(
                  "${(mTick / 1000 + 1).toInt()}S",
                  style: const TextStyle(color: KColors.white),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    if (timerUtil != null) {
      timerUtil.cancel();
    }
  }
}
